home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / steadystate / SteadyState_Setup_ENU.exe / welcome.wsf < prev    next >
Encoding:
Extensible Markup Language  |  2007-04-17  |  21.0 KB  |  304 lines

  1.  ■<?xml version="1.0" ?>
  2. <package>
  3.     <comment>
  4.         ' *** 
  5.         ' *** ------------------------------------------------------------------------------
  6.         ' *** Filename:        Welcome.wsf
  7.         ' *** ------------------------------------------------------------------------------
  8.         ' *** Description:    Command line tool to enable/disable user accounts
  9.         ' *** ------------------------------------------------------------------------------
  10.         ' *** Version:        1.0
  11.         ' *** Notes:        
  12.         ' *** ------------------------------------------------------------------------------
  13.         ' *** Copyright (C) Microsoft Corporation 2005, All Rights Reserved
  14.         ' *** ------------------------------------------------------------------------------
  15.         ' *** 
  16.     </comment>
  17.     <job>
  18.     <runtime>
  19.         <description>Welcome Script</description>
  20.         <named name="enable"    required="false"  many="false"  helpstring="Adds the user account to Windows Welcome." />
  21.         <named name="disable"    required="false"  many="false"  helpstring="Removes the user account from Windows Welcome." />
  22.         <example>Example: Welcome.wsf /enable user</example>
  23.     </runtime>
  24.     
  25.         <resource id="IncorrectUsage">Incorrect Usage Detected</resource>
  26.         <resource id="ScriptName">Welcome.wsf</resource>
  27.         <resource id="EnableSuccess">The specified account has been added to Windows Welcome.</resource>
  28.         <resource id="DisableSuccess">The specified account has been removed from Windows Welcome.</resource>
  29.         <resource id="DomainMember1">This computer is part of a Windows domain.</resource>
  30.         <resource id="DomainMember2">The Windows Welcome screen will not display.</resource>
  31.         <resource id="InvalidUser">The specified account is not valid.</resource>
  32.         <resource id="AdminErrorMsg">You have to be an administrator to use this script.</resource>
  33.         <resource id="Enabled">The specified account has already been added to Windows Welcome.</resource>
  34.         <resource id="Disabled">The specified account has already been removed from Windows Welcome.</resource>
  35.         <resource id="CScriptMessage">Restarting script in command-line mode. Run CmdOn.BAT to set command-line mode as the default mode.</resource>
  36.         <resource id="CScriptTitle">Windows SteadyState: Windows Script Mode Detected</resource>
  37.         <resource id="FailToInit">Initialize objects failure, script will now quit.</resource>
  38.                 
  39.     <?job error="True" debug="False" ?>
  40.     
  41.         <script language="VBScript">
  42.         <![CDATA[
  43.             ' ~~~ 
  44.             ' ~~~ Force variables to be declared 
  45.             ' ~~~ 
  46.             Option Explicit
  47.             
  48.             ' ~~~ 
  49.             ' ~~~ Declare variables and constants
  50.             ' ~~~ 
  51.             Dim oWelcome, bOK, args, bResult
  52.             Dim bLocalUser, bDisabledUser, strRegPath
  53.             
  54.             Dim oShell, oWMIService, oNetwork, oUser
  55.             Dim sComputer
  56.             ' ~~~ Set the registry path
  57.             strRegPath = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon\SpecialAccounts\UserList\"
  58.             ' ~~~ Initialize objects
  59.             bOK = InitializeAllObjects
  60.             If Not(bOK) Then
  61.                 WScript.Echo getResource("FailToInit")
  62.                 Wscript.Quit()
  63.             End If
  64.             
  65.             ' ~~~ ------------------------------------------------------------------------------
  66.             ' ~~~ Check script usage
  67.             ' ~~~ ------------------------------------------------------------------------------
  68.             bOK = True
  69.             Set args = WScript.Arguments
  70.             Select Case args.Count
  71.                 Case 0 ,1,3
  72.                     bOK = False
  73.             End Select
  74.             
  75.             
  76.             If Not(bOK) Then
  77.                 WScript.Arguments.Showusage
  78.                 Wscript.Quit()
  79.             End If
  80.             
  81.             ' ~~~ ------------------------------------------------------------------------------
  82.             ' ~~~ Now do enable/disable
  83.             ' ~~~ ------------------------------------------------------------------------------
  84.             
  85.             ' ~~~ Enable the account 
  86.             If Wscript.Arguments.Named.Exists("enable") Then
  87.                 bOK = True
  88.                 bResult = EnableUser(args(1))
  89.                 If bResult = 0 Then
  90.                         WScript.Echo getResource("EnableSuccess")
  91.                         If DomainMember then 
  92.                             WScript.Echo getResource("DomainMember1")
  93.                             WScript.Echo getResource("DomainMember2")
  94.                         End If
  95.                 ElseIf bResult = 1 Then
  96.                         WScript.Echo getResource("InvalidUser")
  97.                         WScript.Arguments.Showusage
  98.                 ElseIf bResult = 2 Then    
  99.                         WScript.Echo getResource("Enabled")
  100.                         If DomainMember then 
  101.                             WScript.Echo getResource("DomainMember1")
  102.                             WScript.Echo getResource("DomainMember2")
  103.                         End If
  104.                 End If                
  105.             End If
  106.             
  107.             ' ~~~ Disable the account
  108.             If Wscript.Arguments.Named.Exists("disable") Then
  109.                 bOK = True
  110.                 bResult = DisableUser(args(1))
  111.                 If bResult = 0 Then
  112.                     WScript.Echo getResource("DisableSuccess")
  113.                     If DomainMember then 
  114.                         WScript.Echo getResource("DomainMember1")
  115.                         WScript.Echo getResource("DomainMember2")
  116.                     End If
  117.                 ElseIf bResult = 1 Then
  118.                     WScript.Echo getResource("InvalidUser")
  119.                     WScript.Arguments.Showusage
  120.                 ElseIf bResult = 2 Then
  121.                     WScript.Echo getResource("Disabled")
  122.                     If DomainMember then 
  123.                         WScript.Echo getResource("DomainMember1")
  124.                         WScript.Echo getResource("DomainMember2")
  125.                     End If
  126.                 End If
  127.             End If
  128.     
  129.             ' ~~~ If user input the wrong command, echo the usage        
  130.             If Not(Wscript.Arguments.Named.Exists("enable")) AND Not(Wscript.Arguments.Named.Exists("disable")) Then
  131.                 WScript.Arguments.Showusage
  132.             End If
  133.             
  134.             ' ~~~ Destroy objects
  135.             UnLoadObjects()
  136.             Set oWelcome = nothing
  137.             ' ***
  138.             ' *** --------------------------------------------------------------------------------
  139.             ' *** Name:         SetUser()
  140.             ' *** --------------------------------------------------------------------------------
  141.             ' *** Purpose:        Sets and checks the user that we want to add or remove from
  142.             ' ***            the welcome screen
  143.             ' *** --------------------------------------------------------------------------------
  144.             ' ***
  145.             Sub SetUser(strUser)
  146.                 On Error Resume Next
  147.                 Dim strDisabled
  148.                 ' Check if this user is local or not
  149.                 Set oUser = GetObject("WinNT://" & sComputer & "/" & strUser & ",user")
  150.                 If Err.Number <> 0 Then
  151.                     ' ~~~ Couldn't find the user, doesn't exist
  152.                     bLocalUser = False
  153.                 Else
  154.                     ' ~~~ User exists!
  155.                     bLocalUser = True
  156.                 End If
  157.                 ' Check if this user is disabled or not
  158.                 strdisabled = oShell.RegRead(strRegPath & strUser)
  159.                 If IsEmpty(strdisabled) Then
  160.                     bDisabledUser = False
  161.                 Else
  162.                     bDisabledUser = True
  163.                 End If    
  164.             End Sub
  165.             ' ***
  166.             ' *** ------------------------------------------------------------------------------
  167.             ' *** Name:        EnableUser
  168.             ' *** ------------------------------------------------------------------------------
  169.             ' *** Purpose:    Enables the user account, by removing the account from the
  170.             ' ***        UserList Key of the SpecialAccounts in the reigstry.
  171.             ' *** ------------------------------------------------------------------------------
  172.             ' ***
  173.             Function EnableUser(strUser)
  174.                 On Error Resume Next
  175.                 SetUser(strUser)
  176.                 If bLocalUser Then
  177.                     If bDisabledUser = False Then 
  178.                         ' ~~~ Account already disabled
  179.                         EnableUser = 2
  180.                     Else
  181.                         ' ~~~ Add account to Special Accounts list
  182.                         Call oShell.RegDelete(strRegPath & strUser)
  183.                         EnableUser = 0
  184.                     End If
  185.                 Else
  186.                     ' ~~~ Not a local account
  187.                     EnableUser = 1
  188.                 End If
  189.             End Function
  190.             ' ***
  191.             ' *** ------------------------------------------------------------------------------
  192.             ' *** Name:        DisableUser
  193.             ' *** ------------------------------------------------------------------------------
  194.             ' *** Purpose:    Disables the user account, by adding the account in the
  195.             ' ***        UserList Key of the SpecialAccounts in the  reigstry.
  196.             ' *** ------------------------------------------------------------------------------
  197.             ' ***
  198.             Function DisableUser(strUser)
  199.                 On Error Resume Next
  200.                 SetUser(strUser)
  201.                 If bLocalUser Then
  202.                     If bDisabledUser = True Then
  203.                         ' ~~~ Account already disabled
  204.                         DisableUser = 2
  205.                     Else
  206.                         ' ~~~ Disable the account by adding it to the Special Accounts list
  207.                         Call oShell.RegWrite(strRegPath & strUser, 0, "REG_DWORD")
  208.                         DisableUser = 0
  209.                     End If
  210.                 Else
  211.                     ' ~~~ Not a local account
  212.                     DisableUser = 1
  213.                 End If
  214.             End Function
  215.             ' *** 
  216.             ' *** ------------------------------------------------------------------------------
  217.             ' *** Name:            DomainMember()
  218.             ' *** ------------------------------------------------------------------------------
  219.             ' *** Purpose:        Determines if machine is a member of a Windows Domain
  220.             ' *** ------------------------------------------------------------------------------
  221.             ' *** 
  222.             Function DomainMember()
  223.                 On Error Resume Next
  224.                 Dim oComputer, oComputers
  225.                 Set oComputers  = oWMIService.ExecQuery("Select DomainRole from Win32_ComputerSystem")
  226.                 For Each oComputer in oComputers
  227.                     If oComputer.DomainRole=1 Then
  228.                         DomainMember = True
  229.                     Else
  230.                         DomainMember = False
  231.                     End If
  232.                 Next
  233.             End Function
  234.             
  235.             ' *** 
  236.             ' *** ------------------------------------------------------------------------------
  237.             ' *** Name:            InitializeAllObjects()
  238.             ' *** ------------------------------------------------------------------------------
  239.             ' *** Purpose:        This function Initialises all the objects required
  240.             ' *** ------------------------------------------------------------------------------
  241.             ' *** 
  242.             Function InitializeAllObjects()
  243.                 On Error Resume Next
  244.                 Dim bReturn
  245.                 bReturn = True
  246.                 Set oShell = createobject("wscript.shell")
  247.                 If TypeName(oShell) <> "IWshShell3" Then 
  248.                     bReturn = False
  249.                 End If                    
  250.                 
  251.                 Set oNetwork = CreateObject("Wscript.Network")
  252.                 If TypeName(oNetwork) <> "IWshNetwork2" Then 
  253.                     bReturn = False
  254.                 End If                    
  255.                 sComputer  = oNetwork.ComputerName
  256.                 
  257.                 Set oWMIService = GetObject("winmgmts:" _
  258.                     & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
  259.                 If TypeName(oWMIService) <> "SWbemServicesEx" Then 
  260.                     bReturn = False
  261.                 End If
  262.                 
  263.                 
  264.                 InitializeAllObjects = bReturn
  265.             End Function
  266.             
  267.             ' *** 
  268.             ' *** ------------------------------------------------------------------------------
  269.             ' *** Name:            UnLoadObjects()
  270.             ' *** ------------------------------------------------------------------------------
  271.             ' *** Purpose:        This function Uninitialises all the objects
  272.             ' *** ------------------------------------------------------------------------------
  273.             ' *** 
  274.             Function UnLoadObjects()
  275.                 
  276.                 Set oUser = Nothing
  277.                 Set oShell = Nothing
  278.                 Set oNetwork = Nothing    
  279.                 Set oWMIService = Nothing
  280.                 
  281.             End Function
  282.         ]]>
  283.         </script>
  284.     </job>
  285. </package>